package com.lxz.jucdemo.blocking_queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 刘新智
 * @Date: 2025/10/26 11:26
 * @Description:
 * @Version: v1.0
 */
public class BlockingQueueTest {
    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        //test3();
        //test4();
        test5();
    }

    /**
     * @description: 抛出异常
     * @author: 刘新智
     * @date: 2025/10/26 11:29
     * @param: []
     * @return: void
     **/
    public static void test1() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.add(1);
        blockingQueue.add(2);
        blockingQueue.add(3);
//        blockingQueue.add(4);

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }


    /**
     * @description: 不抛出异常，有返回值
     * @author: 刘新智
     * @date: 2025/10/26 11:29
     * @param: []
     * @return: void
     **/
    public static void test2() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer(1);
        blockingQueue.offer(2);
        blockingQueue.offer(3);
//        blockingQueue.offer(4);

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }


    /**
     * @description: 阻塞，一直阻塞
     * @author: 刘新智
     * @date: 2025/10/26 11:29
     * @param: []
     * @return: void
     **/
    public static void test3() throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put(1);
        blockingQueue.put(2);
        blockingQueue.put(3);
//        blockingQueue.put(4);

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }

    /**
     * @description: 超时阻塞，超时返回
     * @author: 刘新智
     * @date: 2025/10/26 11:37
     * @param: []
     * @return: void
     **/
    public static void test4() throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer(1,2, TimeUnit.SECONDS);
        blockingQueue.offer(2, 2,TimeUnit.SECONDS);
        blockingQueue.offer(3, 2,TimeUnit.SECONDS);
//        blockingQueue.offer(4, 2,TimeUnit.SECONDS);

        blockingQueue.poll( 2,TimeUnit.SECONDS);
        blockingQueue.poll( 2,TimeUnit.SECONDS);
        blockingQueue.poll( 2,TimeUnit.SECONDS);
        blockingQueue.poll( 2,TimeUnit.SECONDS);
    }

    /**
     * @description: 同步队列，没有容量，只有put和take方法
     * @author: 刘新智
     * @date: 2025/10/26 11:37
     * @param: []
     * @return: void
     **/
    public static void test5() throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new SynchronousQueue<>();
        new Thread(()->{
            try {

                System.out.println(Thread.currentThread().getName()+" put 1");
                blockingQueue.put(1);

                System.out.println(Thread.currentThread().getName()+" put 2");
                blockingQueue.put(2);

                System.out.println(Thread.currentThread().getName()+" put 3");
                blockingQueue.put(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"1").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get 1");
                blockingQueue.take();

                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get 2");
                blockingQueue.take();

                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" get 3");
                blockingQueue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"2").start();
    }


}

